package cn.softeem.tushu.utils;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.SQLException;

public class TxQueryRunner extends QueryRunner {
    @Override
    public int[] batch(String sql, Object[][] params) throws SQLException {
        Connection connection = DruidPool.getConnection();
        int[] result = super.batch(connection, sql, params);
        DruidPool.relaseConnection(connection);
        return result;
    }

    @Override
    public <T> T query(String sql, ResultSetHandler<T> rsh, Object... params) throws SQLException {
        Connection connection = DruidPool.getConnection();
        T result = super.query(connection, sql, rsh, params);
        DruidPool.relaseConnection(connection);
        return result;
    }

    @Override
    public <T> T query(String sql, ResultSetHandler<T> rsh) throws SQLException {
        Connection connection = DruidPool.getConnection();
        T result = super.query(connection, sql, rsh);
        DruidPool.relaseConnection(connection);
        return result;
    }

    @Override
    public int update(String sql) throws SQLException {
        Connection connection = DruidPool.getConnection();
        int result = super.update(connection, sql);
        DruidPool.relaseConnection(connection);
        return result;
    }

    @Override
    public int update(String sql, Object param) throws SQLException {
        Connection connection = DruidPool.getConnection();
        int result = super.update(connection,sql,param);
        DruidPool.relaseConnection(connection);
        return result;
    }

    @Override
    public int update(String sql, Object... params) throws SQLException {
        Connection connection = DruidPool.getConnection();
        int result = super.update(connection, sql,params);
        DruidPool.relaseConnection(connection);
        return result;
    }

    @Override
    public <T> T query( String sql, Object[] params, ResultSetHandler<T> rsh) throws SQLException {
        Connection connection = DruidPool.getConnection();
        T result = super.query(connection, sql,params, rsh);
        DruidPool.relaseConnection(connection);
        return result;
    }


}
